package com.jspica.app.messagespam;

import java.sql.SQLException;
import java.util.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.*;
import android.content.pm.*;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.net.*;
import android.os.Bundle;
import android.telephony.*;
import android.util.*;
import android.view.*;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.*;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.jspica.app.messagespam.R;
import com.jspica.app.messagespam.BlockedListDAO;
import com.jspica.app.messagespam.MyCursorAdapter;

public class BlackList extends Activity {

	private ListView keywordList;
	private EditText txtKeyword;
	private Cursor cursor;
	private Button btnAdd;
	private int selected_id;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.keywordlist);
			
		setTitle(getResources().getString(R.string.app_name) + " - " +
			getResources().getString(R.string.set_blacklist));
				
		initiateMembers();
		setEventHandlers();
		registerForContextMenu(keywordList);
	}

	@Override
	protected void onResume() {
		super.onResume();
		populateList();		
	}

	
	
	/**
	 * 검색조건에 맞는 신용카드 사용 내역을 조회하여 어뎁터로 할당한다.
	 * @throws SQLException 
	 */
	private void populateList() {

		releaseResources();

		ListAdapter adapter;
		BlockedListDAO manager = new BlockedListDAO(getBaseContext());
		try {
			cursor = manager.getBlacklist();
		} catch (SQLException e) {}
		
		startManagingCursor(cursor);

		adapter = new MyCursorAdapter(this, R.layout.keywordrow, cursor, new String[] {"_id", "keyword"}, 
				new int[] {R.id._id, R.id.viewKeyword});

		//Log.e("@@@@@@@@@@@@@@@#1","count :" + adapter.getCount());
		keywordList.setAdapter(adapter);
		
		keywordList.setOnItemClickListener(itemClick);
		
		manager.close();
		manager = null;
	}
	
	
	AdapterView.OnItemClickListener itemClick = new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView parent, View view, int position, long id) {
			// 버튼이 아닌 아이템 클릭시 이벤트임....
			//Log.e("@@@@@@@@@@@@@@","clickecd");
			
			TextView textview = (TextView)view.findViewById(R.id._id);
			selected_id = Integer.parseInt((String)textview.getText());
						
			new AlertDialog.Builder(BlackList.this)
			.setTitle(getResources().getString(R.string.confirm))
			.setMessage(getResources().getString(R.string.do_delete))
			.setCancelable(false) // Back버튼 작동안하도록.
			.setPositiveButton(getResources().getString(R.string.yes),
				new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
												
					BlockedListDAO dao = new BlockedListDAO(getBaseContext());
					dao.deleteBlacklist(selected_id);
					dao.close();
					populateList();
				}			
			})
			.setNegativeButton(getResources().getString(R.string.no), null)
			.show();
		}
	};
	

	private void releaseResources() {
		if (cursor != null) {
			cursor.close();
			cursor = null;
		}
	}

	/**
	 * 레이어웃의 요소들을 가져와 멤버변후에 바인딩 시킨다.
	 */
	private void initiateMembers() {
		keywordList = (ListView)findViewById(R.id.keywordList);
		txtKeyword = (EditText)findViewById(R.id.txtKeyword);
		btnAdd = (Button)findViewById(R.id.btnAdd);
	}
		
	/**
	 * 버튼들에 대한 클릭 이벤트 리스너를 등록한다.
	 */
	private void setEventHandlers() {
		btnAdd.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String keyword = txtKeyword.getText().toString().trim();
				//Log.e("@@@@1", "keyword :" + keyword);
				if (!keyword.equals("")) {
					//Log.e("@@@@2", "keyword :" + keyword);
					BlockedListDAO dao = new BlockedListDAO(getBaseContext());
					dao.insertBlacklist(keyword);
					dao.close();
					populateList();
				}
			}
		});
	}
}